/*
 * @Author: error: error: git config user.name & please set dead value or install git && error: git config user.email & please set dead value or install git & please set dead value or install git
 * @Date: 2025-03-13 12:03:43
 * @LastEditors: error: error: git config user.name & please set dead value or install git && error: git config user.email & please set dead value or install git & please set dead value or install git
 * @LastEditTime: 2025-03-17 12:04:38
 * @FilePath: /boost_searcher/util.hpp
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
#pragma once
#include <iostream>
#include <string>
#include <fstream>
#include <boost/algorithm/string.hpp>
#include "cppjieba/Jieba.hpp"
namespace ns_util
{
    class FileUtil
    {
    public:
        static bool ReadFile(const std::string &file_path, std::string *out)
        {
            std::ifstream file(file_path.c_str(), std::ios::in); // 来创建输入文件流，用于读取文件
            if (!file.is_open())
            {
                std::cerr << "open file failed! file_path=" << file_path << std::endl;
                return false;
            }
            std::string line;
            while (std::getline(file, line)) // //如何理解getline读取到文件结束呢？？getline的返回值是一个&，while(bool), 本质是因为重载了强制类型转化
            {
                *out += line;
            }
            file.close();
            return true;
        }
    };
    class StringUtil
    {
    public:
        static void Split(const std::string &str, std::vector<std::string> *out, const std::string &sep)
        {
            // boost split
            boost::split(*out, str, boost::is_any_of(sep), boost::token_compress_on); // aaa\3\3\3\3\3\cccccc    token_compress_on表示要对中间的内容进行压缩
        }
    };

    const char *const DICT_PATH = "./dict/jieba.dict.utf8";
    const char *const HMM_PATH = "./dict/hmm_model.utf8";
    const char *const USER_DICT_PATH = "./dict/user.dict.utf8";
    const char *const IDF_PATH = "./dict/idf.utf8";
    const char *const STOP_WORD_PATH = "./dict/stop_words.utf8";

    // 建立软链接：ln -s ~/boost_searcher/dict   cppjieba/dict/
    // 建立软链接：ln -s ~/boost_searcher/cppjieba   cppjieba/include/cppjieba/
    class JiebaUtil
    {
    public:
        // 成员函数可不可以直接调用静态成员变量？？
        // 静态成员变量是属于类的，不是属于对象的，所以不能直接调用，需要通过类名::静态成员变量
        static void CutString(const std::string& src,std::vector<std::string>* out) 
        {
            jieba.CutForSearch(src,*out);
        }

    private:
        static cppjieba::Jieba jieba;
    };
    cppjieba::Jieba JiebaUtil::jieba(DICT_PATH, HMM_PATH, USER_DICT_PATH, IDF_PATH, STOP_WORD_PATH);
};